package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.AddDeptDto;
import com.blueocean_health.care.management.domain.dto.DeptManagerDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.service.DepartmentService;
import com.github.pagehelper.PageInfo;

/**
 * 获取科室信息 接口 2017年8月30日15:05:22
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/Department")
public class DepartmentController{

	@Resource
	private DepartmentService deptService;
	/**
	 * 根据医院Id获取该医院下的科室信息 2017年8月30日15:11:41
	 * @return
	 */
	@RequestMapping("getDepartment")
	public SysResult getDeptByHospitalId(){
    	User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		
		List<PullDownDto> dept = deptService.getDeptByHospitalId(courrentUser.getHospitalId());
//		List<PullDownDto> dept = deptService.getDeptByHospitalId("1");
		return SysResult.ok(0, "success", dept);
	}
	/**
	 * 根据筛选条件查询科室信息 2018年1月6日15:11:13
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getDeptManagerList(@RequestBody Map<String,String> map){
		PageInfo<DeptManagerDto> deptManagerList = deptService.getDeptManagerList(map);
		Map<String,Object> resultMap = new HashMap<>(10);
		Map<String,Object> pageMap = new HashMap<>(10);
		resultMap.put("list", deptManagerList.getList());
		pageMap.put("total", deptManagerList.getTotal());
		resultMap.put("pageInfo", pageMap);
		return SysResult.ok(0, "success", resultMap);
	}
	/**
	 * 根据科室Id查询科室信息  2018年1月6日15:54:22
	 * @return
	 */
	@RequestMapping("query")
	public SysResult getDeptManagerById(@RequestBody Map<String,String> map){
		AddDeptDto deptManagerById = deptService.getDeptManagerById(map);
		return SysResult.ok(0, "success", deptManagerById);
	}
	/**
	 * 根据科室Id查询科室信息  2018年1月6日15:54:22
	 * @return
	 */
	@RequestMapping("update")
	public SysResult updateDept(@RequestBody AddDeptDto addDept){
		SysResult updateDept = deptService.updateDept(addDept);
		return updateDept;
	}
	/**
	 * 根据科室Id查询科室信息  2018年1月6日15:54:22
	 * @return
	 */
	@RequestMapping("insert")
	public SysResult insertDept(@RequestBody AddDeptDto addDept){
		SysResult insertDept = deptService.insertDept(addDept);
		return insertDept;
	}
	/**
	 * 删除科室信息  2018年1月9日15:32:00
	 * @return
	 */
	@RequestMapping("delete")
	public SysResult deleteDept(@RequestBody DeptManagerDto deptDto){
		SysResult insertDept = deptService.deleteDept(deptDto);
		return insertDept;
	}
	/**
	 * 根据筛选条件导出科室信息 Excel  2018年1月9日18:05:32
	 * @param map
	 * @return
	 */
	@RequestMapping("excel")
	public void getDeptListExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>(10);
			String deptName = request.getParameter("deptName");
			String buildingId = request.getParameter("buildingId");
			String floorId = request.getParameter("floorId");
			if(!StringUtils.isEmpty(deptName)){
				deptName = deptName.trim();
				map.put("deptName", deptName);
			}
			map.put("buildingId", buildingId);
			map.put("floorId", floorId);
			List<DeptManagerDto> deptListExcel = deptService.deptListExcel(map);
			String finalFileName = URLEncoder.encode("科室管理_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, deptListExcel, DeptManagerDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
